package com.example.easyexcel.excelNew.controller;

import com.example.easyexcel.excelNew.EasyExcelUtils;
import com.example.easyexcel.excelNew.ExcelImportErrDto;
import com.example.easyexcel.excelNew.UserImportVo;
import com.example.easyexcel.excelNew.listenner.EasyExcelListener;
import com.example.easyexcel.excelNew.service.UserSerVice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class excelController {
    @Autowired
    UserSerVice userSerVice;

    @GetMapping(value = "importExcel")
    public void uploadFile(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        try {
            // 参数1 response 错误导出使用 参数2 导入文件  参数3 导入对应的实体对象
//            EasyExcelUtils.webImportExcel(response, file.getInputStream(), UserImportVo.class);
            //第四个参数是否导出错误数据 默认为true 设置false 可以自己处理错误数据

//            EasyExcelListener easyExcelListener = EasyExcelUtils.webImportExcel(response,file.getInputStream(), UserImportVo.class,false);
            EasyExcelListener easyExcelListener = EasyExcelUtils.webImportExcel(response, file.getInputStream(), userSerVice, UserImportVo.class);
//            // 获取错误数据结果集
            List<ExcelImportErrDto> errorList = easyExcelListener.getErrList();
            //获取正确数据结果集
            List<UserImportVo> successList = easyExcelListener.getSuccessList();
            // 第三个参数 ExcelCheckManager实现自主校验 正常在service接口继承 实现自主校验和存入数据库
            System.out.println("-------------------"+errorList);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

        @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        EasyExcelUtils.writeExcel12(response,null,"用户信息","用户信息", UserImportVo.class);
    }
}
